﻿[
    {
        $project: {
            "loa": "$$ROOT",
            "_id": 0
        }
    },
    {
        $lookup: {
            localField: "loa.people",
            from: "peoples",
            foreignField: "_id",
            as: "p"
        }
    },
    {
        $unwind: {
            path: "$p",
            preserveNullAndEmptyArrays: true
        }
    },
    {
        $lookup: {
            localField: "loa.absence_code",
            from: "absences",
            foreignField: "absence_type_code",
            as: "ab"
        }
    },
    {
        $unwind: {
            path: "$ab",
            preserveNullAndEmptyArrays: true
        }
    },
    {
        $match: {
            $and: [{
                "loa.absence_code": {
                    $ne: null
                }
            },
            {
                "loa.create_end_date": {
                    $gte: new ISODate('@CalcStartTime'),
                    $lte: new ISODate('@CalcEndTime')
                }
            }
            ]
        }
    },
    {
        $project: {
            "people_no": "$p.people_no",
            "people_name": "$p.people_name",
            "_id": "$loa._id",
            "create_start_date": "$loa.create_start_date",
            "create_end_date": "$loa.create_end_date",
            "date_arr": "$loa.total_detail.date",
            "start_time_arr": "$loa.total_detail.start",
            "end_time_arr": "$loa.total_detail.end",
            "leave_hours_arr": "$loa.total_detail.leave_hours",
            "absence_type_name": "$ab.absence_type_name",
            "leave_days": "$loa.leave_days",
            "hours": "$loa.hours",
            "leave_reason": "$loa.leave_reason"
        }
    }
]